#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# ======================================================
# @File:      : file_model
# @Author     : forward_huan
# @Date       : 2025/6/4 9:10
# @Desc       :
# ======================================================
import time
from datetime import datetime

import sqlite_tool.sqlite_data
from sqlite_tool import sqlite_data
from sqlite_tool.model.db_model import DBModel
from sqlite_tool.utils import get_dict_value


class FileModel(DBModel):
    table_name = "upload_file"

    def __init__(self, data: dict = None):
        super(FileModel, self).__init__()
        self.id = get_dict_value(data, "id", None)
        self.file_time = get_dict_value(data, "file_time", time.time())
        self.save_path = get_dict_value(data, "save_path", "")
        self.state = get_dict_value(data, "state", 0)
        self.ctime = get_dict_value(data, "ctime", time.time())
        self.mtime = get_dict_value(data, "mtime", time.time())

    @staticmethod
    def create_sql_str():
        return f"""
            create table {FileModel.table_name}(
                id integer primary key         autoincrement,
                file_time      real        not null,
                save_path      char(100)   not null,
                state          int  not null,
                ctime          real        not null,
                mtime          real        not null
            );
        """

    @property
    def fields(self):
        return ["id", "file_time", "save_path", "state", "mtime", "ctime"]

    def save(self):
        sqlite_data.add(self.table_name, data=self.db_json)

    def exist(self, fields):
        return sqlite_data.exist(self.table_name, where_dict={"file_time": self.file_time, "state": self.state})
